package com.watermeter.android.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.oa.android.R;
import com.watermeter.android.MainActivity;
import com.watermeter.android.utils.UConstants;
import com.watermeter.android.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 登录
 * Created by zouqh on 2016/5/23.
 */
public class LoginActivity extends Activity implements View.OnClickListener {
    private Button login;
    private EditText userName;
    private EditText userPass;
    private CheckBox checkBox;
    private TextView setting;
    ProgressDialog dialog;

    private Util util;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.Ext.init(getApplication());
        // 设置是否输出debug
        x.Ext.setDebug(true);
        setContentView(R.layout.login_activity);
        login = (Button) findViewById(R.id.login);
        userName = (EditText) findViewById(R.id.userName);
        userPass = (EditText) findViewById(R.id.userPass);
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        setting=(TextView)findViewById(R.id.setting);
        login.setOnClickListener(this);
        setting.setOnClickListener(this);
        util = new Util(this);
        String name = util.getUser("name");
        String password = util.getUser("password");
        String mindpass = util.getUser("mindpass");
        String strIp=util.getUser("ip");
        String strPost=util.getUser("post");
        Util.printLog("strIp-----------------", strIp);
        Util.printLog("strPost-----------------", strPost);
        if (Util.NoNull(strIp)&&Util.NoNull(strPost)){
            UConstants.WEB_URL="http://"+strIp+":"+strPost+"/BLL/";
            new UConstants().setIp();
        }else{
            Toast.makeText(getApplicationContext(),"请设置服务器地址！",Toast.LENGTH_SHORT).show();
        }
        if (Util.NoNull(name)) {
            userName.setText(name);
        }
        if (Util.NoNull(mindpass)&&mindpass.equals("1")) {
            checkBox.setChecked(true);
            if (Util.NoNull(password)) {
                userPass.setText(password);
            }

        }

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.login:
                String n = userName.getText().toString();
                String p = userPass.getText().toString().trim();
                if (n.length() <= 0) {
                    Toast.makeText(x.app(), "请输入用户名！", Toast.LENGTH_LONG).show();
                } else if (p.length() <= 0) {
                    Toast.makeText(x.app(), "请输入密码！", Toast.LENGTH_LONG).show();
                } else {
                    if (Util.NoNull(UConstants.WEB_URL)) {
                        dialog = ProgressDialog.show(this, null, "登录中...", true, false);
                        Login(n, p);
                    }else{
                        Toast.makeText(getApplicationContext(),"请设置服务器地址！",Toast.LENGTH_SHORT).show();
                    }

                }

//                intent.setClass(this, MainActivity.class);
//                startActivity(intent);
//                finish();
                break;

            case R.id.setting:
                intent.setClass(this, SaveServiceActivity.class);
                intent.putExtra("type","1");
                startActivity(intent);
                break;
        }
    }


    //用户登录
    private void Login(final String name, final String pass) {
        RequestParams params = new RequestParams(UConstants.LOGIN_URL);
        params.addBodyParameter("LKind", "CBJ");
        params.addBodyParameter("EmpId", name);
        params.addBodyParameter("Pwd", pass);
//        Map<String,String> map=new HashMap<>();
//        map.put("LKind", "CBJ");
//        map.put("EmpId", name);
//        map.put("Pwd", pass);
//        HttpHandler httpHandler=new HttpHandler();
//       String result= httpHandler.HttpPost(map,UConstants.LOGIN_URL);
//        Util.printLog("Login------------onSuccess-----", result);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
                        String msg = object.getString("msg");
                        if (success) {
                            if (checkBox.isChecked()) {
                                util.setUser(name, pass, "1");
                            } else {
                                util.setUser(name, pass, "0");
                            }
                            UConstants.EmpId = object.getString("EmpId");
                            UConstants.EmpName = object.getString("EmpName");
                            UConstants.RoleId = object.getString("RoleId");
                            UConstants.RoleName = object.getString("RoleName");
                            UConstants.Powers = object.getString("Powers");

                            if(object.toString().contains("ShowTBSL")){
                                UConstants.ShowTBSL= object.getString("ShowTBSL");
                            }
//                            UConstants.DepId = object.getString("DepId");
                            if(object.toString().contains("DepName")){
                                UConstants.DepName = object.getString("DepName");
                            }
                            if(object.toString().contains("ComName")){
                                UConstants.ComName = object.getString("ComName");
                            }

//                            intent.setClass(this, MainActivity.class);
                            msg="登录成功！";
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        }

                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "地址错误或服务器无法连接！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
